using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Game.Framework.Utils;
using Newtonsoft.Json;
using UnityEngine;


namespace Game.Framework.Localization
{
    public class LanguageConfigManager : Singleton<LanguageConfigManager>
    {
        Dictionary<string, Dictionary<string, string>> m_languageConfigs = new Dictionary<string, Dictionary<string, string>>();
        
        public void InitConfigs(string path)
        {
            foreach (string locale in LocalizationManager.Instance.SupportedLocale)
            {
                if (m_languageConfigs.ContainsKey(locale))
                    continue;
                
                var filePath = $"{path}/locale_{locale}.json";
                LogUtil.Log("filePath = {0}",filePath);
                var hander = YooAssetManager.Instance.Package.LoadAssetSync<TextAsset>(filePath); 
                var ta = hander.AssetObject as TextAsset; 
                var content = ta.text;
                
                var localeConfig = JsonConvert.DeserializeObject<List<LanguageConfig>>(content);
                if (localeConfig != null && localeConfig.Count > 0)
                {
                    Dictionary<string, string> configs = new Dictionary<string, string>();
                    foreach (LanguageConfig c in localeConfig)
                    {
                        if (string.IsNullOrEmpty(c.Key))
                        {
                            LogUtil.LogError("key is " + c.Key + "  value is " + c.Value);
                            continue;
                        }

                        if (configs.ContainsKey(c.Key))
                        {
                            configs[c.Key] = c.Value;
                        }
                        else
                        {
                            configs.Add(c.Key, c.Value);
                        }
                    }

                    m_languageConfigs[locale] = configs;
                }
            }
        }
        
        string ReplaceKeyVlaue(Dictionary<string, Dictionary<string, string>> tempLocaleConfigs, string locale, string key)
        {
            string value = tempLocaleConfigs[locale][key];
            if (string.IsNullOrEmpty(value))
            {
                LogUtil.LogWarning("Arthur--->GetLocalString value empty! key = {0}",key);
                return "";
            }
            
            MatchCollection Matches = Regex.Matches(value, "%{(.+?)}");
            foreach (Match match in Matches)
                value = value.Replace("%{" + match.Groups[1].Value + "}", GetLocalString(match.Groups[1].Value, locale));

            return value;
        }

        public string GetLocalString(string key, string locale)
        {
            if (m_languageConfigs.ContainsKey(locale) && m_languageConfigs[locale].ContainsKey(key))
            {
                return ReplaceKeyVlaue(m_languageConfigs, locale, key);
            }
            
            return key;
        }
    }
    
}



